<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="采购人" prop="enterName">
                      <el-input v-model="formData.enterName" placeholder="请输入" />
                    </el-form-item>
					<el-form-item label="采购时间" prop="enterDate">
					  <el-date-picker clearable v-model="formData.enterDate" type="datetime" value-format="timestamp" placeholder="选择时间" />
					</el-form-item>
                   <!-- <el-form-item label="报账人电话" prop="enterPhone">
                      <el-input v-model="formData.enterPhone" placeholder="请输入报账人电话" />
                    </el-form-item> -->
                    <el-form-item label="采购类目" prop="type">
                      <el-select v-model="formData.type" placeholder="请选择类目">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.TEAM_LEDGER_TYPE)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="商品名称" prop="productName">
                      <el-input v-model="formData.productName" placeholder="请输入商品名称" />
                    </el-form-item>
                    <el-form-item label="单价" prop="unitPrice">
                      <el-input v-model="formData.unitPrice" placeholder="请输入单价" />
                    </el-form-item>
					<el-form-item label="数量" prop="column1">
					  <el-input v-model="formData.column1" placeholder="请输入" />
					</el-form-item>
                    <el-form-item label="小计" prop="money">
                      <el-input v-model="formData.money" placeholder="请输入小计" />
                    </el-form-item>
                   <el-form-item label="审核状态" prop="approvalStatus" v-if="formData.id">
                      <el-select v-model="formData.approvalStatus" placeholder="请选择审核状态">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.TEAM_LEDGER_APPROVAL_STATUS)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <!-- <el-form-item label="审核人" prop="approvalName">
                      <el-input v-model="formData.approvalName" placeholder="请输入审核人" />
                    </el-form-item>-->
                    <el-form-item label="审核备注" prop="approvalRemarks"  v-if="formData.id">
                      <el-input v-model="formData.approvalRemarks" placeholder="请输入审核备注" />
                    </el-form-item> 
					<el-form-item label="备注" prop="remarks">
					  <el-input v-model="formData.remarks" placeholder="请输入备注" />
					</el-form-item>
                    <el-form-item label="上传凭证">
                      <ImageUpload v-model="formData.url"/>
                    </el-form-item>
                    
                   <!-- <el-form-item label="自定义" prop="column1">
                      <el-input v-model="formData.column1" placeholder="请输入自定义" />
                    </el-form-item>
                    <el-form-item label="自定义" prop="column2">
                      <el-input v-model="formData.column2" placeholder="请输入自定义" />
                    </el-form-item>
                    <el-form-item label="自定义" prop="column3">
                      <el-input v-model="formData.column3" placeholder="请输入自定义" />
                    </el-form-item>
                    <el-form-item label="自定义" prop="column4">
                      <el-input v-model="formData.column4" placeholder="请输入自定义" />
                    </el-form-item>
                    <el-form-item label="自定义" prop="column5">
                      <el-input v-model="formData.column5" placeholder="请输入自定义" />
                    </el-form-item>
                    <el-form-item label="自定义" prop="column6">
                      <el-input v-model="formData.column6" placeholder="请输入自定义" />
                    </el-form-item> -->
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as LedgerApi from '@/api/team/ledger';
  import ImageUpload from '@/components/ImageUpload';
      export default {
    name: "LedgerForm",
    components: {
          ImageUpload,
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            enterDate: undefined,
                            enterName: undefined,
                            enterPhone: undefined,
                            type: undefined,
                            productName: undefined,
                            unitPrice: undefined,
                            money: undefined,
                            approvalStatus: undefined,
                            approvalName: undefined,
                            approvalRemarks: undefined,
                            url: undefined,
                            remarks: undefined,
                            column1: undefined,
                            column2: undefined,
                            column3: undefined,
                            column4: undefined,
                            column5: undefined,
                            column6: undefined,
        },
        // 表单校验
        formRules: {
			enterDate: [
			    { required: true, message: '请输入', trigger: 'blur' },
			],
			enterName: [
			    { required: true, message: '请输入', trigger: 'blur' },
			],
			unitPrice: [
						    { required: true, message: '请输入', trigger: 'blur' },
							{ pattern: /^\d+(\.\d+)?$/, message: '请输入有效的数字', trigger: 'blur' }
						],
			type: [
						    { required: true, message: '请选择', trigger: 'blur' },
						],
			money: [
						    { required: true, message: '请输入', trigger: 'blur' },
							{ pattern: /^\d+(\.\d+)?$/, message: '请输入有效的数字', trigger: 'blur' }
						],
			column1: [
						    { required: true, message: '请输入', trigger: 'blur' },
						],
			productName: [
						    { required: true, message: '请输入', trigger: 'blur' },
						],													
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await LedgerApi.getLedger(id);
            this.formData = res.data;
			this.formData.unitPrice=(this.formData.unitPrice/100).toFixed(2)
			this.formData.money=(this.formData.money/100).toFixed(2)
            this.title = "修改报账管理";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增报账管理";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
		
          const data = this.formData;
		    data.unitPrice= data.unitPrice*100
			data.money= data.money*100
                  // 修改的提交
          if (data.id) {
            await LedgerApi.updateLedger(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await LedgerApi.createLedger(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            enterDate: undefined,
                            enterName: undefined,
                            enterPhone: undefined,
                            type: undefined,
                            productName: undefined,
                            unitPrice: undefined,
                            money: undefined,
                            approvalStatus: undefined,
                            approvalName: undefined,
                            approvalRemarks: undefined,
                            url: undefined,
                            remarks: undefined,
                            column1: undefined,
                            column2: undefined,
                            column3: undefined,
                            column4: undefined,
                            column5: undefined,
                            column6: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
